iT邦幫忙

0

Gzip Command in Linux with Examples

  • 分享至 

  • xImage
  •  

Today you will learn how to compress files in Linux using gzip command.

What is File Compression?

File compression is a method by which we can save disk space by reducing the size of available data. A compressed file is a collection in which one or more files are stored.

Why do we need to Compress a file or directory?

Compressed files have many advantages. Here I have explained some of them:

  • The compressed file uses less Harddisk space.

  • When you plan to send multiple files via email (assuming there are 100 files), it is not the right way to attach each file individually as an email attachment and This may exceed the attachment size allowed by your email administrator.

Hence always you should send such files in a compressed way which not only reduces the size of the file but you can send all those files in an organized way.

  • When we transfer naked files through the Internet, the chances of it getting corrupted increases. This is why you should always compress and transfer your important files.

We have several ways in Linux by which you can compress files and directories. The most popular tools are:

  • gzip
  • bzip2
  • zip

In this guide, we are going to discuss the complete features of gzip command with examples.

gzip is the most popular compression tool among other similar tools used to compress files only.

But by combining it with the tar command, you can also compress directories.

Features of gzip Command:

  • Compress and Decompress (Extract) a File
  • Reduces file size
  • Display the contents of a compressed file
  • Tests compressed file integrity
  • Lists each compressed File

How to identify a gzip file?

You can identify the gzip file by its extension. The file extension of gzip is .gz

Syntax:

You have to follow the below syntax to use gzip command.

gzip [OPTION]... [FILE]...

1. Compress a File

To compress a file using gzip type the following command.

Here I am compressing a file named file.txt.

~$ gzip file.txt

Output:

~$ ls
file.txt.gz

2. Compress multiple Files

You can compress multiple files using gzip.

To compress multiple files, pass the file names to gzip. Here I am compressing files named file.txt, file1.txt, file2.txt, file3.txt, file4.txt

~$ gzip file.txt file1.txt file2.txt file3.txt file4.txt

Output:

~$ ls
file1.txt.gz  file2.txt.gz  file3.txt.gz  file4.txt.gz  file.txt.gz

You can also use wildcards with gzip. Let’s take some examples:

Ex. # 1 Compress those files whose extension is “.txt“.

~$ gzip *.txt

Ex. # 2 Compress those files that start with “fi“.

~$ gzip fi*

3. Keep (Don’t delete) input Files

gzip by default deletes the input file after compressing.

But if you want to keep the input files during compression, then pass the -k option to gzip.

~$ gzip -k file.txt

Output:

~$ ls
file.txt  file.txt.gz

By the way, the -c option helps to concatenate the contents of multiple files.

But you can also use this option to keep input files while compressing.

Here is an example.

Syntax:

gzip -c [INPUT FILE] > [OUTPUT FILE]

~$ gzip -c file.txt > newfile.txt.gz

Now type the following command to concatenate the contents of the two files.

Here in this example, I am concatenating the contents of file.txt and file1.txt.

~$ gzip -c file1.txt >> newfile.txt.gz

Now to decompress the file type the following command.

~$ gzip -d newfile.txt.gz

Output:

~$ ls
newfile.txt
~$ cat newfile.txt 
Linux is a Open Source Operating System.
I Love Linux

Read Complete Article Here


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言